package com.whales.lms.rms.web.controller;

import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.output.XMLOutputter;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whales.lms.rms.web.model.FaultVO;
import com.whales.lms.rms.web.service.FaultService;
import com.whales.lms.rms.web.util.ConstantUtil;
import com.whales.shared.controller.GenericController;
import com.whales.shared.util.XmlUtil;

@Controller
@RequestMapping("/fault/*")
public class FaultController extends GenericController<FaultVO, FaultVO, FaultService> {
	/* Agent에서 호출되는 Software 상태 정보를 parameter로 보내온다.
	 * @see com.whales.shared.controller.GenericController#create(java.lang.Object)
	 */
	@Override
	@RequestMapping
	public ResponseEntity<String> create(FaultVO paramVo) throws Exception {
		paramVo.setSepType(ConstantUtil.TYPE_SW);
		paramVo.setFaultCode(paramVo.getState());
		Document doc;
		try{
			if(ConstantUtil.STATE_OKE.equalsIgnoreCase(paramVo.getFaultCode()) 
					|| ConstantUtil.STATE_SPE.equalsIgnoreCase(paramVo.getFaultCode())
					|| ConstantUtil.STATE_SFE.equalsIgnoreCase(paramVo.getFaultCode())){
				super.create(paramVo);
				doc = XmlUtil.buidDocForServerResponse(ConstantUtil.STATE_OKE);
			}else{
				throw new Exception();
			}
		}catch(Exception e){
			e.printStackTrace();
			doc = XmlUtil.buidDocForServerResponse(ConstantUtil.STATE_ERR);
		}
		return super.getResponseEntity(new XMLOutputter().outputString(doc));
	}
	
	@RequestMapping
	public @ResponseBody List<Map<String, String>> getCount(String countType) throws Exception{
		if(ConstantUtil.CHART_DAILY.equals(countType)){
			return service.getDayilyCount();
		}else if(ConstantUtil.CHART_WEEKLY.equals(countType)){
			return service.getWeeklyCount();
		}else if(ConstantUtil.CHART_MONTHLY.equals(countType)){
			return service.getMonthlyCount();
		}else{
			return null;
		}
	}
}
